import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AesTest {

    public static void main(String[] args) throws Exception{

        //ak:testkey    sk:secretkey

        JSONObject json = new JSONObject();
        json.put("mobile","1:iIdOdHNd4Ju7KATwegaYT9H1aIiqZi8rzmKYV3609/k=");
        json.put("channelCode","4000100");

       String token = new AkSkUtils("testkey", "secretkey")
               .generateToken("/login/freePwdLogin", "POST", null, json.toJSONString(), 1587627952);

       System.out.println(token);

        String phone = new AesEcbUtils("1:6a6c4f66df581405768223954baae2e3").encrypt("13581782134");

        System.out.println(phone);

        //v2-testkey-1587627952-0ff81fb3b0b4fbb86814163d6f05f517
        //v2-testkey-1587627952-0ff81fb3b0b4fbb86814163d6f05f517

        //v2-testkey-1587627952-45e097d1569ac57a1694272e066fe819

        String ss = send("https://mallqa.weshare.com.cn/capybara/login/freePwdLogin",null,"utf-8");
    }


    /**
     * 模拟请求
     *
     * @param url        资源地址
     * @param map    参数列表
     * @param encoding    编码
     * @return
     * @throws ParseException
     * @throws IOException
     */
    public static String send(String url, Map<String,String> map, String encoding) throws ParseException, IOException{
        String body = "";

        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);



        JSONObject jsonParam = new JSONObject();
        jsonParam.put("mobile", "1:iIdOdHNd4Ju7KATwegaYT9H1aIiqZi8rzmKYV3609/k=");
        jsonParam.put("channelCode", "4000100");
        StringEntity entityJson = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
        entityJson.setContentEncoding("UTF-8");
        entityJson.setContentType("application/json");

        //设置参数到请求对象中
        httpPost.setEntity(entityJson);

        System.out.println("请求地址："+url);

        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("X-WeshareAuth-Token", "v2-testkey-1587627952-0ff81fb3b0b4fbb86814163d6f05f517");

        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = client.execute(httpPost);
        //获取结果实体
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            //按指定编码转换结果实体为String类型
            body = EntityUtils.toString(entity, encoding);

            System.out.println("body : " + body);
        }
        EntityUtils.consume(entity);
        //释放链接
        response.close();
        return body;
    }


}
